package com.asen.commons.utils;

import com.google.common.collect.Lists;
import org.springframework.lang.Nullable;

import java.util.Collection;
import java.util.List;

/**
 * CollectionUtils
 *
 * @author Gary
 * @version 1.0
 * @date 2022-03-28
 */
public class CollectionUtils extends org.springframework.util.CollectionUtils {

    public static boolean isNotEmpty(@Nullable Collection<?> collection) {
        return !isEmpty(collection);
    }

    public static <T> boolean addAll(Collection<? super T> c, Collection<T> collection) {
        boolean result = false;
        for (T element : collection) {
            result |= c.add(element);
        }
        return result;
    }

    public static <T> boolean containsAny(final Collection<?> coll1, @SuppressWarnings("unchecked") final T... coll2) {
        if (coll1.size() < coll2.length) {
            for (final Object aColl1 : coll1) {
                if (ArrayUtils.contains(coll2, aColl1)) {
                    return true;
                }
            }
        } else {
            for (final Object aColl2 : coll2) {
                if (coll1.contains(aColl2)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isAllEmpty(Collection<?>... lists) {
        if (lists.length == 0) {
            return true;
        }
        boolean isEmpty = true;
        for (Collection<?> list : lists) {
            isEmpty &= isEmpty(list);
        }
        return isEmpty;
    }

    /**
     * 如果数组为 null 则 返回 []
     * @param list
     * @param <T>
     * @return
     */
    public static <T> List<T> sureList(List<T> list) {
        if (list == null) {
            return  Lists.newArrayList();
        }
        return list;
    }

}
